package gui;

import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import daten.Professor;
import daten.ProfessorenContainer;

public class ProfessorenListe extends Dialog implements ActionListener, Observer
{
	List lbProfessoren;

	public ProfessorenListe(Frame owner) {
		super(owner, "Professoren-Liste");
		this.setLayout(new BorderLayout());
		
		this.add(new Label("Professoren-Liste"), BorderLayout.NORTH);
		
		this.lbProfessoren = new List();
		
		this.add(lbProfessoren, BorderLayout.CENTER);
		
		Panel q = new Panel();
		q.setLayout(new GridLayout(1,2));
		
		Button b = new Button("Editieren");
		b.addActionListener(this);
		q.add(b);

		Button c = new Button("Abbrechen");
		c.addActionListener(this);
		q.add(c);
		
		Iterator<Professor> it = ProfessorenContainer.instance().iterator();
		while(it.hasNext()) {
			lbProfessoren.add(it.next().toString());
		}
		
		this.add(q,BorderLayout.SOUTH);
		
		this.pack();
		this.setSize(600,400);
		this.setVisible(true);
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("Abbrechen")) {
			this.dispose();
		}
		if(arg0.getActionCommand().equals("Editieren")) {
			if(lbProfessoren.getSelectedItems().length == 1) {
				new ProfessorEditieren(this, ProfessorenContainer.instance().getProfessor(lbProfessoren.getSelectedIndex()));
			}
		}
		
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		this.lbProfessoren.removeAll();
		Iterator<Professor> it = ProfessorenContainer.instance().iterator();
		while(it.hasNext()) {
			lbProfessoren.add(it.next().toString());
		}
	}
}
